import time, sys, gc
import numpy as np
import pandas as pd
import uuid
import csv
import math
import wbgapi as wb

import warnings, logging
sys.path.append('./')
# 配置日志
from utils.file_unrar import *
from utils.load_access_data import *
from utils.data_mapping_36country3 import *
from utils.file_process import *

from utils.logging_config import setup_logger


logger = setup_logger('Bash Mapping')

class Config:
    target_cols = ['channelno', 'dataid', 'iesign', 'datatype', 'writeoffflag'
            , 'writeoffdataid', 'outputdate', 'origincountrycode', 'origincountry', 'countrycodeofdelivery'
            , 'countryofdelivery', 'importername', 'importeraddress', 'importercontact', 'suppliername'
            , 'supplieraddress', 'suppliercontact', 'hscode', 'hscodedescription', 'commoditydescription'
            , 'totalcifvalue', 'totalfobvalue', 'grossweight', 'netweight', 'quantity'
            , 'quantityunit', 'teu', 'importer_forwarderagent', 'supplier_forwarderagent', 'abnormaldata'
            , 'portofloading', 'portofdestination', 'loadingcountrycode', 'loadingcountry', 'transportterm'
            , 'tradeterm', 'paymentterm', 'carrier', 'containerno', 'vesselname'
            , 'brand', 'version', 'country', 'IMPORTER_ID', 'SUPPLIER_ID', 'cif_currency', 'fob_currency']
    chunk_size=50000
config = Config

# 加载36个国家码
data_country36_code = pd.read_csv('./param_files/数据36国家代码表.csv')
data_country36_dict = data_country36_code.to_dict('records')

country36_dict = dict()
for x in data_country36_dict:
    country36_dict[x.get('中文国名')]={'英文国名': x.get('英文国名'),'国别编码':x.get('国别编码')}
    
# 加载各国映射函数   
function_map = get_function_map(data_country36_dict)


def bash_mapping_export(country_list, from_dir, to_dir, all_country_cols_dict):
    # 获取文件目录，区分出口、进口数据
    root_dir = from_dir
    dir_paths, file_paths = get_dir_files(root_dir)
    file_tree = distinct_ie_data(root_dir, country_list)

    # csv结果文件保存路径
    csv_files_path = to_dir
    # csv_files_path
    
    # 出口跑批
    start = time.time()
    for ctry in country_list:
        logger.info(ctry)
        if file_tree[ctry]['export_paths']:
            for exp_path in file_tree[ctry]['export_paths']:
                logger.info(exp_path)
                exp_file = str(exp_path).split(os.sep)[-1]
                func_map = [x for x in function_map if x.get('中文国名')==ctry]
                target_tab = func_map[0].get('export_fuc')(exp_path, all_country_cols_dict[ctry]['export_paths'])
                # 目标表保存至csv
                logger.info(f'target_tab shape: {target_tab.shape}')
                save_data_to_csv(target_tab, csv_files_path, file_name=exp_file, country_code=ctry, IE_tag='EXPORT')
                # print(target_tab.head())
                del target_tab
                gc.collect()
    end = time.time()
    logger.info(f'all data takes {round((end - start)/60, 2)} minutes')
    return len(country_list)


def bash_mapping_import(country_list, from_dir, to_dir, all_country_cols_dict):
    # 获取文件目录，区分出口、进口数据
    root_dir = from_dir
    dir_paths, file_paths = get_dir_files(root_dir)
    file_tree = distinct_ie_data(root_dir, country_list)
    # csv结果文件保存路径
    csv_files_path = to_dir
    
    # 进口跑批
    start = time.time()
    for ctry in country_list:
        logger.info(ctry)
        if file_tree[ctry]['import_paths']:
            # N = len(file_tree[ctry]['import_paths'])
            for i in range(len(file_tree[ctry]['import_paths'])):
                imp_path = file_tree[ctry]['import_paths'][i]
                logger.info(imp_path)
                imp_file = str(imp_path).split(os.sep)[-1]
                func_map = [x for x in function_map if x.get('中文国名')==ctry]
                target_tab = func_map[0].get('import_fuc')(imp_path, all_country_cols_dict[ctry]['import_paths'])
                # 目标表保存至csv
                logger.info(f'target_tab shape: {target_tab.shape}')
                save_data_to_csv(target_tab, csv_files_path, file_name=imp_file, country_code=ctry, IE_tag='IMPORT')
                del target_tab
                gc.collect()
    end = time.time()
    logger.info(f'all data takes {round((end - start)/60, 2)} minutes')
    return len(country_list)


def bash_mapping_export2(country_list, from_dir, to_dir, all_country_cols_dict):
    # 获取文件目录，区分出口、进口数据
    root_dir = from_dir
    dir_paths, file_paths = get_dir_files(root_dir)
    file_tree = distinct_ie_data(root_dir, country_list)

    # csv结果文件保存路径
    csv_files_path = to_dir
    # csv_files_path
    
    # 出口跑批
    start = time.time()
    for ctry in country_list:
        logger.info(ctry)
        # print(file_tree[ctry]['export_paths'])
        if file_tree[ctry]['export_paths']:
            for exp_path in file_tree[ctry]['export_paths']:
                logger.info(exp_path)
                exp_file = str(exp_path).split(os.sep)[-1]
                func_map = [x for x in function_map if x.get('中文国名')==ctry]
                target_tab = func_map[0].get('export_fuc')(exp_path, all_country_cols_dict[ctry]['export_paths'])
                # 目标表保存至csv
                cols = ['country', 'iesign', 'version', 'dataid', 'importername', 'suppliername']
                target_tab = target_tab.loc[:,cols]
                logger.info(f'target_tab shape: {target_tab.shape}')
                save_data_to_csv(target_tab, csv_files_path, file_name=exp_file, country_code=ctry, IE_tag='EXPORT')
                # print(target_tab.head())
                del target_tab
                gc.collect()
    end = time.time()
    logger.info(f'all data takes {round((end - start)/60, 2)} minutes')
    returnfo(f'all data takes {round((end - start)/60, 2)} minutes')
    return len(country_list)


def bash_mapping_import2(country_list, from_dir, to_dir, all_country_cols_dict):
    # 获取文件目录，区分出口、进口数据
    root_dir = from_dir
    dir_paths, file_paths = get_dir_files(root_dir)
    file_tree = distinct_ie_data(root_dir, country_list)
    # csv结果文件保存路径
    csv_files_path = to_dir
    
    # 进口跑批
    start = time.time()
    for ctry in country_list:
        logger.info(ctry)
        if file_tree[ctry]['import_paths']:
            # N = len(file_tree[ctry]['import_paths'])
            for i in range(0, len(file_tree[ctry]['import_paths'])):
                imp_path = file_tree[ctry]['import_paths'][i]
                logger.info(imp_path)
                imp_file = str(imp_path).split(os.sep)[-1]
                func_map = [x for x in function_map if x.get('中文国名')==ctry]
                target_tab = func_map[0].get('import_fuc')(imp_path, all_country_cols_dict[ctry]['import_paths'])
                # 目标表保存至csv
                cols = ['country', 'iesign', 'version', 'dataid', 'importername', 'suppliername']
                target_tab = target_tab.loc[:,cols]
                logger.info(f'target_tab shape: {target_tab.shape}')
                save_data_to_csv(target_tab, csv_files_path, file_name=imp_file, country_code=ctry, IE_tag='IMPORT')
                del target_tab
                gc.collect()
            logger.info(f'country {ctry} done!')
    end = time.time()
    logger.info(f'all data takes {round((end - start)/60, 2)} minutes')
    return len(country_list)










##
